package com.bonc.ioc.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * Json工具类
 * 1、获取所有的key
 * 2、获取第一级的所有key
 */
public class JsonUtils {

    /**
     * 递归读取所有的key
     * @param jsonObject jsonObject
     * @return StringBuffer key1|key2|key3|
     */
    public static StringBuffer getAllKey(JSONObject jsonObject) {
        StringBuffer stringBuffer = new StringBuffer();
        // jsonObject.keys()
        Iterator<String> keys = jsonObject.keySet().iterator();
        while (keys.hasNext()) {
            String key = keys.next();
            stringBuffer.append(key.toString()).append("|");
            if (jsonObject.get(key) instanceof JSONObject) {
                JSONObject innerObject = (JSONObject) jsonObject.get(key);
                stringBuffer.append(getAllKey(innerObject));
            } else if (jsonObject.get(key) instanceof JSONArray) {
                JSONArray innerObject = (JSONArray) jsonObject.get(key);
                stringBuffer.append(getAllKey(innerObject));
            }
        }
        return stringBuffer;
    }

    /**
     * 递归读取所有的key
     * @param jsonArray jsonArray
     * @return StringBuffer key1|key2|key3|
     */
    public static StringBuffer getAllKey(JSONArray jsonArray) {
        StringBuffer stringBuffer = new StringBuffer();
        if (jsonArray != null ) {
            Iterator i1 = jsonArray.iterator();
            while (i1.hasNext()) {
                Object key = i1.next();
                if (key instanceof  JSONObject) {
                    JSONObject innerObject = (JSONObject) key;
                    stringBuffer.append(getAllKey(innerObject));
                } else if (key instanceof JSONArray) {
                    JSONArray innerObject = (JSONArray) key;
                    stringBuffer.append(getAllKey(innerObject));
                }
            }
        }
        return stringBuffer;
    }

    /**
     * 读取所有的key
     * @param jsonObject jsonObject
     * @return List<String> [key1, key2, key3]
     */
    public static List<String> getAllKeyList(JSONObject jsonObject) {
        StringBuffer allKeyString = getAllKey(jsonObject);
        List<String> list = Arrays.asList(allKeyString.toString().split("\\|"));
        return new LinkedList<>(list);
    }

    /**
     * 读取所有的key
     * @param jsonString json字符串
     * @return List<String> [key1, key2, key3]
     */
    public static List<String> getAllKeyList(String jsonString) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return getAllKeyList(jsonObject);
    }

    /**
     * 读取第一级的所有key
     * @param jsonObject jsonObject
     * @return StringBuffer key1|key2|key3|
     */
    public static StringBuffer getFirstLevelKey(JSONObject jsonObject) {
        StringBuffer stringBuffer = new StringBuffer();
        // jsonObject.keys()
        Iterator<String> keys = jsonObject.keySet().iterator();
        while (keys.hasNext()) {
            String key = keys.next();
            stringBuffer.append(key.toString()).append("|");
        }
        return stringBuffer;
    }

    /**
     * 读取第一级的所有key
     * @param jsonObject jsonObject
     * @return List<String> [key1, key2, key3]
     */
    public static List<String> getFirstLevelKeyList(JSONObject jsonObject) {
        StringBuffer allKeyString = getFirstLevelKey(jsonObject);
        List<String> list = Arrays.asList(allKeyString.toString().split("\\|"));
        return new LinkedList<>(list);
    }

    /**
     * 读取第一级的所有key
     * @param jsonString json字符串
     * @return List<String> [key1, key2, key3]
     */
    public static List<String> getFirstLevelKeyList(String jsonString) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return getFirstLevelKeyList(jsonObject);
    }
}
